﻿using System;
using System.Windows.Documents;
using System.Windows.Markup;
using VisualWip.Core;
using VisualWip.Core.Columns;
using VisualWip.Core.Interfaces;

namespace VisualWip.PresentationModel.Text
{
    public class RichTextColumnSettingsViewModel : SettingsViewModelBase<RichTextColumn>
    {
        public RichTextColumnSettingsViewModel(Application application, RichTextColumn column, IColumnTypeProvider columnTypeProvider)
            : base(application, column, columnTypeProvider)
        {
        }

        public override string SettingsWindowName
        {
            get { return "Text column settings"; }
        }

        public void SaveDocument(FlowDocument document)
        {
            try
            {
                Column.Document = XamlWriter.Save(document);
            }
            catch (Exception)
            {
                //TODO: Add generic error handling here
                Column.Document = XamlWriter.Save(new FlowDocument());
            }
        }

        public FlowDocument GetDocument()
        {
            if (!string.IsNullOrWhiteSpace(Column.Document))
            {
                try
                {
                    return XamlReader.Parse(Column.Document) as FlowDocument;
                }
                catch (Exception)
                {
                    //TODO: Add generic error handling here
                }
            }
            return new FlowDocument();

        }
    }
}